#include "Datafilter.h"


float AverageFilter(AverageFilter_CB* var, float data)
{

    float res = 0.0f;
    if (var->index < var->bufsize)
    {
        var->buf[var->index] = data;
        var->lastSum += data;
        var->index++;
        res = var->lastSum / var->index;
    }
    else
    {
        var->lastSum -= var->buf[0];
        for (size_t i = 0; i < var->bufsize - 1; i++)
        {
            var->buf[i] = var->buf[i + 1];
        }
        var->buf[var->bufsize - 1] = data;
        var->lastSum += data;
        res = var->lastSum / var->bufsize;
    }

    return  res;
}


